﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ShootMan
{
    public class GameRoot : MonoBehaviour
    {
        public class GameLayerOpenArg : shaco.Base.BaseEventArg
        {
            public GameRoot root;
        }

        public int currentScore = 0;

        public Transform enemySpawnPoint;
        public Transform randomSpawnMinPositionX;
        public Transform randomSpawnMaxPositionX;
        public Transform playersRoot;

        private int _totalShootCount = 0;
        private Dictionary<string, DefaultPlayerController> _players = new Dictionary<string, DefaultPlayerController>();
        static private GameRoot _instance = null;

        void Awake()
        {
            _instance = this;

            //创建初始角色（自己）
            this.Delay(() => 
            {
                CheckAutoAddPlayer(UserData.playerID);
            }, 1.0f);
        }

        void Start()
        {
            //绑定射击分数变化并支持自动释放的事件
            this.AddAutoReleaseEvent<ShootAddScoreEvent>(OnShootScoreEventCallBack);
            this.AddAutoReleaseEvent<ShootBulletCountRecordEvent>(OnShootCountEventCallBack);

            //打开游戏主UI
            shaco.GameHelper.ui.OpenUI<GameLayer>(new GameLayerOpenArg() { root = this });
        }

        static public Transform GetPlayersRoot()
        {
            if (null == _instance)
            {
                shaco.Log.Error("GameRoot GetPlayersRoot error: instance is null");
                return null;
            }
            return _instance.playersRoot;
        }

        static public void DestroyRoot()
        {
            if (null == _instance)
                return;
            MonoBehaviour.Destroy(_instance.gameObject);
            _instance = null;
        }

        static public DefaultPlayerController CheckAutoAddPlayer(string playerID)
        {
            DefaultPlayerController retValue = null;
            if (null == _instance)
            {
                shaco.Log.Error("GameRoot CheckAutoAddPlayer error: instance is null");
                return retValue;
            }

            if (_instance._players.TryGetValue(playerID, out retValue))
            {
                return retValue;
            }
            var loadPrefab = shaco.GameHelper.res.LoadEx<GameObject>($"{GlobalDefines.HOTUPDATE_ROOT}Prefabs/Players/DefaultPlayer.prefab");
            var playerPrefab = loadPrefab.GetComponent<DefaultPlayerController>();
            playerPrefab.playerID = playerID;
            playerPrefab = MonoBehaviour.Instantiate(playerPrefab);
            playerPrefab.transform.SetParent(_instance.playersRoot, false);
            _instance._players.Add(playerPrefab.playerID, playerPrefab);
            return retValue;
        }

        private void OnShootScoreEventCallBack(object sender, ShootAddScoreEvent arg)
        {
            currentScore += arg.score;

            //当分数低于0时候失败
            if (currentScore < 0)
            {
                //发送一个事件然后测试在lua中监听并实现效果，打开失败界面
                this.InvokeEvent(new GameOverEvent()
                {
                    totalShootCount = _totalShootCount
                });
            }
        }

        private void OnShootCountEventCallBack(object sender, ShootBulletCountRecordEvent arg)
        {
            _totalShootCount += arg.shootCount;
        }
    }
}